package com.example.demo.account.service.impl;

import com.example.demo.account.entity.SysUser;
import com.example.demo.account.entity.SysUserRole;
import com.example.demo.account.repository.UserRoleRepository;
import com.example.demo.account.service.UserRoleService;
import com.example.demo.common.utils.Constant;
import com.example.demo.information.entity.School;
import com.example.demo.information.repository.SchoolRepository;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
 * @author littlecar
 * @date 2020/3/16 14:27
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserRoleServiceImpl implements UserRoleService {
    @Autowired
    private UserRoleRepository userRoleRepository;
    @Autowired
    private SchoolRepository schoolRepository;
    @Override
    public SysUserRole findById(Long id) {
        return userRoleRepository.findById(id).get();
    }

    @Override
    public Map<Long, String> combineUserRoles(List<SysUserRole> sysUserRoles) {
        Map<Long,String> map= Maps.newLinkedHashMap();
        if (null != sysUserRoles && sysUserRoles.size()>0) {
            for (SysUserRole userRole :
                    sysUserRoles) {
                if(userRole.getRole_id()>=4 && userRole.getSchool_id()==null){
                    continue;
                }
                String roleNmae = Constant.roleMap.get(Constant.roleIdMap.get(userRole.getRole_id()));
                String userRoleName = "";
                if(userRole.getSchool_id() != null){
                    Optional<School> op = schoolRepository.findById(userRole.getSchool_id());
                    String schoolName = schoolRepository.findById(userRole.getSchool_id()).get().getName();
                    userRoleName=schoolName+"-"+roleNmae;
                }else{
                    userRoleName=roleNmae;
                }
                map.put(userRole.getId(), userRoleName);
            }
            return map;
        }
        return null;
    }

    @Override
    public void save(SysUserRole userRole) {
        userRoleRepository.save(userRole);
    }

    @Override
    public List<SysUserRole> findByUserId(Long id) {
        return userRoleRepository.findByUser_id(id);
    }

    @Override
    public Map<Long, List<SysUserRole>> findMapByUserId(Long id) {
        List<SysUserRole> currUserRoleList = userRoleRepository.findByUser_id(id);
        Map<Long,List<SysUserRole>> roleId_userRoleMap= Maps.newLinkedHashMap();
        if (null !=currUserRoleList && currUserRoleList.size()>0) {
            for (SysUserRole userRole:
                    currUserRoleList) {
                List<SysUserRole> list = roleId_userRoleMap.get(userRole.getRole_id());
                if (null == list) {
                    list = Lists.newArrayList();
                    list.add(userRole);
                }else{
                    list.add(userRole);
                }
                roleId_userRoleMap.put(userRole.getRole_id(), list);
            }
            return roleId_userRoleMap;
        }
        return null;
    }

    @Override
    public void delete(SysUserRole s) {
        userRoleRepository.delete(s);
    }

    @Override
    public void deleteAll(List<SysUserRole> sysUserRoles) {
        userRoleRepository.deleteAll(sysUserRoles);
    }

    @Override
    public void deteleByUserIdAndRoleId(Long user_id, Long role_id) {
        userRoleRepository.deteleByUserIdAndRoleId(user_id, role_id);
    }

    @Override
    public List<SysUserRole> findByUserIdAndRoleId(Long userId, Long roleId) {
        return userRoleRepository.findByUserIdAndRoleId(userId,roleId);
    }

    @Override
    public List<SysUserRole> findAllSchoolAdmin() {
        return userRoleRepository.findAllSchoolAdmin();
    }

    @Override
    public void updateSchoolId(Long user_id, Long school_id, Long role_id) {
        userRoleRepository.updateSchoolId(user_id, school_id,role_id);
    }

    @Override
    public List<Long> findUserIdBySchoolIdAndRoleId(Long school_id, Long role_id) {
        return userRoleRepository.findUserIdBySchoolIdAndRoleId(school_id,role_id);
    }

    @Override
    public SysUserRole findOneByUserIdAndRoleId(Long user_id, Long role_id) {
        return userRoleRepository.findOneByUserIdAndRoleId( user_id,  role_id);
    }

    @Override
    public SysUserRole findByUserIdAndRoleIdAndSchoolId(Long user_id, Long role_id, Long school_id) {
        return userRoleRepository.findByUserIdAndRoleIdAndSchoolId( user_id,  role_id,school_id);
    }

    @Override
    public void deleteByUserIdAndRoleIdAndSchoolId(Long user_id, Long role_id, Long school_id) {
        userRoleRepository.deleteByUserIdAndRoleIdAndSchoolId(user_id,  role_id,  school_id);
    }
}
